This Data Visualization repository is solely developed for the research article mentioned here. This is only for visualization purposes and all the visualization models made from the data sources provided by Indonesia's national disaster management agency. In Indonesia, it called Badan Nasional Penanggulangan Bencana (BNPB). Here we present an approach to visualize and analyze the data of daily statistical covid 19 dataset for each province in Indonesia from its API (access in : https://bnpb-inacovid19.hub.arcgis.com/datasets/statistik-harian-per-provinsi-covid19-indonesia-rev/geoservice)
Table of Content
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import requests
import json
from pandas.io.json import json_normalize
api_bnpb = "https://opendata.arcgis.com/datasets/685be21cd0034247b5ceeac996d947fe_0.geojson" #initialization of url json
request = requests.get(api_bnpb)
request
data = request.json()
df = pd.json_normalize(data,'features', sep = "_")
df.head() #showing the content of the data
df.tail()
#filtering column that you use
df.drop(columns = ['type', 'geometry','properties_Object_ID', 'properties_CFR_Harian','properties_RI_Harian','properties_FID','properties_ObjectId'], inplace = True)
df
#rename colomns
df.rename(columns = {'properties_Provinsi': 'state','properties_Tanggal':'date','properties_Kasus_Terkonfirmasi_Akumulatif': 'Total_Terkonfirmasi', 'properties_Penambahan_Harian_Kasus_Terkonf' : 'Terkonfirmasi_Harian',
'properties_Kasus_Sembuh_Akumulatif': 'Total_Sembuh', 'properties_Penambahan_Harian_Kasus_Sembuh': 'Sembuh_Harian', 'properties_Kasus_Meninggal_Akumulatif':'Total_MeninggalDunia','properties_Penambahan_Harian_Kasus_Meningg':'MeninggalDunia_Harian','properties_Kasus_Aktif_Akumulatif':'Total_Aktif' }, inplace=True)
df
df.isnull().sum()
df.drop(df[df.state=="Indonesia"].index, inplace=True)
df
temp = df.groupby(['date'])['Total_Terkonfirmasi', 'Total_MeninggalDunia', 'Total_Sembuh', 'Total_Aktif'].sum().reset_index()
temp.style.background_gradient(cmap='Pastel1')
temp = df.groupby(['state'])['Total_Terkonfirmasi', 'Total_MeninggalDunia', 'Total_Sembuh', 'Total_Aktif'].sum().reset_index()
temp.style.background_gradient(cmap='Pastel1')
df['date'].max(axis=0)
full_latest = df[df['date'] == max(df['date'])].reset_index()
full_latest
Total_Terkonfirmasi_fix = full_latest['Total_Terkonfirmasi'].sum()
Total_Terkonfirmasi_fix
Total_Sembuh_fix = full_latest['Total_Sembuh'].sum()
Total_Sembuh_fix
Total_MeninggalDunia_fix = full_latest['Total_MeninggalDunia'].sum()
Total_MeninggalDunia_fix
Top10kasus_terkonfirmasi = full_latest.sort_values(by='Total_Terkonfirmasi', ascending=False).head(10)
Top10kasus_terkonfirmasi
Top10kasus_sembuh = full_latest.sort_values(by='Total_Sembuh', ascending=False).head(10)
Top10kasus_sembuh
Top10kasus_meninggaldunia = full_latest.sort_values(by='Total_MeninggalDunia', ascending=False).head(10)
Top10kasus_meninggaldunia
Top10kasus_aktif = full_latest.sort_values(by='Total_Aktif', ascending=False).head(10)
Top10kasus_aktif
fig = px.choropleth(df, geojson='https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia.geojson', locations='state', color='Total_Terkonfirmasi', color_continuous_scale='Reds', featureidkey='properties.state', animation_frame='date', range_color=(0, max(df['Total_Terkonfirmasi'])))
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
fig = px.choropleth(df, geojson='https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia.geojson', locations='state', color='Total_Sembuh', color_continuous_scale='Reds', featureidkey='properties.state', animation_frame='date', range_color=(0, max(df['Total_Terkonfirmasi'])))
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
fig = px.choropleth(df, geojson='https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia.geojson', locations='state', color='Total_MeninggalDunia', color_continuous_scale='Reds', featureidkey='properties.state', animation_frame='date', range_color=(0, max(df['Total_Terkonfirmasi'])))
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
fig = px.choropleth(df, geojson='https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia.geojson', locations='state', color='Total_Aktif', color_continuous_scale='Reds', featureidkey='properties.state', animation_frame='date', range_color=(0, max(df['Total_Terkonfirmasi'])))
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
fig1 = px.bar(Top10kasus_terkonfirmasi, x=Top10kasus_terkonfirmasi['state'], y=Top10kasus_terkonfirmasi['Total_Terkonfirmasi'],
hover_name=Top10kasus_terkonfirmasi['state'],
color=Top10kasus_terkonfirmasi['Total_Terkonfirmasi'], text=Top10kasus_terkonfirmasi['Total_Terkonfirmasi'], height=400)
fig1.update_traces(texttemplate='%{text:}', textposition='outside')
fig1.update_layout(
title={
'text': "Provinsi dengan kasus terkonfirmasi tertinggi di Indonesia",
'y':1,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
# Set the visibility ON
fig1.update_xaxes(title='Provinsi', visible=True, showticklabels=True)
# Set the visibility OFF
fig1.update_yaxes(title='Total', visible=True, showticklabels=True)
fig1.show()
fig11 = px.bar(Top10kasus_sembuh, x=Top10kasus_sembuh['state'], y=Top10kasus_sembuh['Total_Sembuh'],
hover_name=Top10kasus_sembuh['state'],
color=Top10kasus_sembuh['Total_Sembuh'], text=Top10kasus_sembuh['Total_Sembuh'], height=400)
fig11.update_traces(texttemplate='%{text:}', textposition='outside')
fig11.update_layout(
title={
'text': "Provinsi dengan kasus sembuh tertinggi di Indonesia",
'y':1,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
# Set the visibility ON
fig11.update_xaxes(title='Provinsi', visible=True, showticklabels=True)
# Set the visibility OFF
fig11.update_yaxes(title='Total', visible=True, showticklabels=True)
fig11.show()
fig12 = px.bar(Top10kasus_meninggaldunia, x=Top10kasus_meninggaldunia['state'], y=Top10kasus_meninggaldunia['Total_MeninggalDunia'],
hover_name=Top10kasus_meninggaldunia['state'],
color=Top10kasus_meninggaldunia['Total_MeninggalDunia'], text=Top10kasus_meninggaldunia['Total_MeninggalDunia'], height=400)
fig12.update_traces(texttemplate='%{text:}', textposition='outside')
fig12.update_layout(
title={
'text': "Provinsi dengan kasus Sembuh tertinggi di Indonesia",
'y':1,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
# Set the visibility ON
fig12.update_xaxes(title='Provinsi', visible=True, showticklabels=True)
# Set the visibility OFF
fig12.update_yaxes(title='Total', visible=True, showticklabels=True)
fig12.show()
fig13 = px.bar(Top10kasus_aktif, x=Top10kasus_aktif['state'], y=Top10kasus_aktif['Total_Aktif'],
hover_name=Top10kasus_aktif['state'],
color=Top10kasus_aktif['Total_Aktif'], text=Top10kasus_aktif['Total_Aktif'], height=400)
fig13.update_traces(texttemplate='%{text:}', textposition='outside')
fig13.update_layout(
title={
'text': "Provinsi dengan kasus Aktif tertinggi di Indonesia",
'y':1,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
# Set the visibility ON
fig13.update_xaxes(title='Provinsi', visible=True, showticklabels=True)
# Set the visibility OFF
fig13.update_yaxes(title='Total', visible=True, showticklabels=True)
fig13.show()
fig2 = go.Figure(data=[go.Pie(labels=Top10kasus_terkonfirmasi['state'], values=Top10kasus_terkonfirmasi['Total_Terkonfirmasi'],
insidetextorientation='radial',
pull=[0.1, 0, 0, 0, 0, 0])])
fig2.update_traces(textinfo='percent+label')
fig2.show()
fig21 = go.Figure(data=[go.Pie(labels=Top10kasus_sembuh['state'], values=Top10kasus_sembuh['Total_Sembuh'],
insidetextorientation='radial',
pull=[0.1, 0, 0, 0, 0, 0])])
fig21.update_traces(textinfo='percent+label')
fig21.show()
fig22 = go.Figure(data=[go.Pie(labels=Top10kasus_meninggaldunia['state'], values=Top10kasus_meninggaldunia['Total_MeninggalDunia'],
insidetextorientation='radial',
pull=[0.1, 0, 0, 0, 0, 0])])
fig22.update_traces(textinfo='percent+label')
fig22.show()
fig23 = go.Figure(data=[go.Pie(labels=Top10kasus_aktif['state'], values=Top10kasus_meninggaldunia['Total_Aktif'],
insidetextorientation='radial',
pull=[0.1, 0, 0, 0, 0, 0])])
fig23.update_traces(textinfo='percent+label')
fig23.show()
Top10kasus_terkonfirmasi['Tingkat_Penyembuhan'] = Top10kasus_terkonfirmasi['Total_Sembuh'] / Top10kasus_terkonfirmasi['Total_Terkonfirmasi']
Top10kasus_terkonfirmasi['Tingkat_Hidup'] = Top10kasus_terkonfirmasi['Total_MeninggalDunia'] / Top10kasus_terkonfirmasi['Total_Terkonfirmasi']
Top10kasus_terkonfirmasi.index = Top10kasus_terkonfirmasi.index
fig3 = px.scatter(Top10kasus_terkonfirmasi, x='Tingkat_Penyembuhan', y='Tingkat_Hidup',
color='state', size='Total_Terkonfirmasi', hover_name='state',
size_max=30)
fig3.show()
fig4 = px.treemap(Top10kasus_terkonfirmasi.sort_values(by='Total_Terkonfirmasi', ascending=False).reset_index(drop=True),
path=["state"], values="Total_Terkonfirmasi", height=700,
title='Tree Maps Total Terkonfirmasi',
color_discrete_sequence = px.colors.qualitative.Prism)
fig4.data[0].textinfo = 'label+text+value'
fig4.update_layout(margin=dict(t=80,l=0,r=0,b=0))
fig4.show()
fig41 = px.treemap(Top10kasus_sembuh.sort_values(by='Total_Terkonfirmasi', ascending=False).reset_index(drop=True),
path=["state"], values="Total_Sembuh", height=700,
title='Tree Maps Total Sembuh',
color_discrete_sequence = px.colors.qualitative.Prism)
fig41.data[0].textinfo = 'label+text+value'
fig41.update_layout(margin=dict(t=80,l=0,r=0,b=0))
fig41.show()
fig42 = px.treemap(Top10kasus_meninggaldunia.sort_values(by='Total_MeninggalDunia', ascending=False).reset_index(drop=True),
path=["state"], values="Total_MeninggalDunia", height=700,
title='Tree Maps Total Meninggal Dunia',
color_discrete_sequence = px.colors.qualitative.Prism)
fig42.data[0].textinfo = 'label+text+value'
fig42.update_layout(margin=dict(t=80,l=0,r=0,b=0))
fig42.show()
fig43 = px.treemap(Top10kasus_sembuh.sort_values(by='Total_Sembuh', ascending=False).reset_index(drop=True),
path=["state"], values="Total_Sembuh", height=700,
title='Tree Maps Total Sembuh',
color_discrete_sequence = px.colors.qualitative.Prism)
fig43.data[0].textinfo = 'label+text+value'
fig43.update_layout(margin=dict(t=80,l=0,r=0,b=0))
fig43.show()
fig44 = px.treemap(Top10kasus_aktif.sort_values(by='Total_Aktif', ascending=False).reset_index(drop=True),
path=["state"], values="Total_Aktif", height=700,
title='Tree Maps Total Aktif',
color_discrete_sequence = px.colors.qualitative.Prism)
fig44.data[0].textinfo = 'label+text+value'
fig44.update_layout(margin=dict(t=80,l=0,r=0,b=0))
fig44.show()
temp = df.groupby('date').sum().reset_index()
temp['Jumlah Kematian dari 100 Kasus yang Terkonfirmasi'] = round(temp['Total_MeninggalDunia']/temp['Total_Terkonfirmasi'], 3)*100
temp['Jumlah Sembuh dari 100 Kasus yang Terkonfirmasi'] = round(temp['Total_Sembuh']/temp['Total_Terkonfirmasi'], 3)*100
temp = temp.melt(id_vars='date', value_vars=['Jumlah Kematian dari 100 Kasus yang Terkonfirmasi', 'Jumlah Sembuh dari 100 Kasus yang Terkonfirmasi'],
var_name='Ratio', value_name='Value')
fig5 = px.line(temp, x="date", y="Value", color='Ratio', log_y=True,
title='Tingkat Kesembuhan dan Kematian berdasarkan 100 Kasus yang Terkonfirmasi',
color_discrete_sequence=px.colors.qualitative.G10)
fig5.update_layout(legend=dict(orientation="h", y=1, x=0,
xanchor="left", yanchor="top"),
margin=dict(t=80,l=0,r=0,b=0))
fig5.show()
fig6 = go.Figure(go.Line(x=df['date'], y=df['Total_Terkonfirmasi'],name='Terkonfirmasi', mode='lines+markers',marker=dict(size=10,color='indianred')))
fig6.add_trace(go.Line(x=df['date'], y=df['Total_Sembuh'],name='Sembuh', mode='lines+markers',marker=dict(size=10,color='lightseagreen')))
fig6.add_trace(go.Line(x=df['date'], y=df['Total_MeninggalDunia'], name='Meninggal Dunia', mode='lines+markers',marker=dict(size=10,color='gray')))
fig6.add_trace(go.Line(x=df['date'], y=df['Total_Aktif'], name='Aktif', mode='lines+markers',marker=dict(size=10,color='Orange')))
fig6.update_layout(xaxis_showgrid=True, yaxis_showgrid=True, plot_bgcolor='whitesmoke',
title={
'text': 'Line Chart Kasus Covid-19 di Indonesia',
'y':0.75,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'},xaxis_type='category')
fig6.update_xaxes(title= '------>Timeline' ,showline=False)
fig6.update_yaxes(title= '------>jumlah kasus', showline=False)
fig6.show()
fig7 = px.scatter(Top10kasus_terkonfirmasi, x='state', y='Total_Terkonfirmasi')
fig7.show()
fig71 = px.scatter(Top10kasus_meninggaldunia, x='state', y='Total_MeninggalDunia')
fig71.show()
fig72 = px.scatter(Top10kasus_sembuh, x='state', y='Total_Sembuh')
fig72.show()
fig73 = px.scatter(Top10kasus_aktif, x='state', y='Total_Aktif')
fig73.show()
fig = make_subplots(
rows = 4, cols = 6,
specs=[
[{'type': 'scattergeo', 'rowspan': 4, 'colspan': 3}, None, None, {'type': 'indicator'}, {'type': 'indicator'}, {'type': 'indicator'} ],
[ None, None, None, {'type': 'bar', 'colspan':3}, None, None],
[ None, None, None, {'type':'bar', 'colspan':3}, None, None],
[ None, None, None, {'type': 'bar', 'colspan':3}, None, None],
]
)
full_latest['text'] = full_latest['state']
full_latest['text'] += 'Total Terkonfirmasi : ' + full_latest['Total_Terkonfirmasi'].astype(str) + "\n"
full_latest['text'] += 'Total Sembuh : ' + full_latest['Total_Sembuh'].astype(str) + "\n"
full_latest['text'] += 'Total MeninggalDunia : ' + full_latest['Total_MeninggalDunia'].astype(str) + "\n"
full_latest['text'] += 'Total Aktif : ' + full_latest['Total_Aktif'].astype(str) + "\n"
fig.add_trace(
go.Scattergeo(
geojson='https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia.geojson',
featureidkey='properties.state',
locations=full_latest['state'],
hovertext = full_latest['text'],
showlegend=False,
marker = dict(
size = 10,
opacity = 0.8,
reversescale = True,
autocolorscale = True,
symbol = 'square',
line = dict(
width=1,
),
color=full_latest['Total_Terkonfirmasi'],
colorbar_title="Terkonfirmasi",
colorbar_x = -0.05,
)),
row=1, col=1
)
fig.update_layout(
geo_scope='asia',
height=600)
fig.update_geos(fitbounds="locations")
fig.add_trace(
go.Indicator(
mode='number',
value=Total_Terkonfirmasi_fix,
title='Total terkonfirmasi',
),
row=1, col=4
)
fig.add_trace(
go.Indicator(
mode='number',
value=Total_Sembuh_fix,
title='Total sembuh',
),
row=1, col=5
)
fig.add_trace(
go.Indicator(
mode='number',
value=Total_MeninggalDunia_fix,
title='Total meninggal',
),
row=1, col=6
)
fig.add_trace(
go.Bar(
x=Top10kasus_terkonfirmasi['state'],
y=Top10kasus_terkonfirmasi['Total_Terkonfirmasi'],
name= 'Terkonfirmasi',
marker=dict(color='Yellow'),
showlegend=True,
),
row=2, col=4
)
fig.add_trace(
go.Bar(
x=Top10kasus_sembuh['state'],
y=Top10kasus_sembuh['Total_Sembuh'],
name= 'Sembuh',
marker=dict(color='Blue'),
showlegend=True),
row=3, col=4
)
fig.add_trace(
go.Bar(
x=Top10kasus_meninggaldunia['state'],
y=Top10kasus_meninggaldunia['Total_MeninggalDunia'],
name= 'Meninggal',
marker=dict(color='Green'),
showlegend=True),
row=4, col=4
)
Nadya Safitri